<template>
  <div>
    <!--列表 -->
    <el-table :data="list" style="width: 100%">
      <el-table-column label="日期" prop="datetime"> </el-table-column>
      <el-table-column label="名字" prop="name"> </el-table-column>
      <el-table-column label="地址" prop="address"> </el-table-column>
      <el-table-column label="标题" prop="title"> </el-table-column>

      <el-table-column align="right">
        <template slot="header">
          <el-input v-model="search" size="mini" placeholder="输入关键字搜索" />
        </template>

        <template slot-scope="scope">
          <el-button size="mini" @click="handleEdit(scope.$index, scope.row)"
            >编辑</el-button
          >
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 弹框 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="日期" :label-width="formLabelWidth">
          <!-- <el-input placeholder="请选择日期" v-model="form.datetime"> -->
            <el-date-picker
              v-model="form.datetime"
              type="datetime"
              placeholder="选择日期时间"
            >
            </el-date-picker>
          <!-- </el-input> -->
        </el-form-item>
        <el-form-item label="标题" :label-width="formLabelWidth">
          <el-input v-model="form.title" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="地址" :label-width="formLabelWidth">
          <el-input v-model="form.address" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleModify"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { mapState, mapActions, mapMutations } from "vuex";
export default {
  name: 'List',
  computed: {
    ...mapState(["list"])
  },
  created() {
    // this.list.length === 0 && this.GET_LIST();
     this.GET_LIST()
  },
  data() {
    return {
      search: "",
      dialogFormVisible: false,
      form: {
        name: "",
        title: "",
        address: "",
        datetime: "",
        id: ""
      },
      formLabelWidth: "120px"
    };
  },
  methods: {
    ...mapActions(["GET_LIST"]),
    ...mapMutations(["DELETE_LIST", 'MODIFY_LIST']),
    handleEdit(index, row) {
      this.dialogFormVisible = true;
      this.form = { ...row };
    },
    handleDelete(index, row) {
      const { id } = row;

      this.DELETE_LIST(id);
    },
    handleModify() {
      this.dialogFormVisible = false

      this.MODIFY_LIST(this.form)
    }
  }
};
</script>